package
{
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemIdleMode;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageAspectRatio;
	import flash.display.StageOrientation;
	import flash.display.StageScaleMode;
	import flash.events.*;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.ObjectEncoding;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	public class VideoCamera extends Sprite
	{
		private var serverURl:String = "rtmp://192.168.1.202/videoSurveillance";
//		private var serverURl:String = "rtmp://192.168.15.113/videoSurveillance";
		private var connection:NetConnection;
		private var stream:NetStream;
		private var camera:Camera;
		public function VideoCamera()
		{
			super();
			// 支持 autoOrient
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
//			stage.autoOrients = true; //Orientations
			
			NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown );//程序返回就退出
			NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
			NativeApplication.nativeApplication.autoExit = true;
			
			stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE,orientationChange);
			showCamea();
			stage.setAspectRatio(StageAspectRatio.LANDSCAPE);
			stage.autoOrients = false;
			stage.setOrientation(StageOrientation.ROTATED_RIGHT);
			
		}
		private function orientationChange(evt:StageOrientationEvent):void{
			trace(evt);
			switch (evt.afterOrientation) {
				case StageOrientation.DEFAULT:
					trace("DEFAULT");
					break;
				case StageOrientation.ROTATED_RIGHT:
					trace("ROTATED_RIGHT");
					break;
				case StageOrientation.ROTATED_LEFT:
					trace("ROTATED_LEFT");
					break;
				case StageOrientation.UPSIDE_DOWN:
					trace("UPSIDE_DOWN");
					break;
			}
//			showCamea();

//			var t:TextField = new TextField();
//			t.x = 
//			t.ad
		}
		private function showCamea():void{
			var t:TextField = new TextField();
			t.x = 0;
			t.y = 0;
			t.width =50;
			t.height = 50;
			t.text = "asfasgdhdfjfgjfgjfrsgasgasg";
			addChild(t);
			trace(stage.orientation);
			camera = Camera.getCamera();
			camera.setMode(320, 240, 15);
			camera.setQuality(0,87);
//			camera.
			var vid:Video = new Video();//default 320*240
			vid.attachCamera(camera);
			vid.x = 0;
			vid.y = 0;
			vid.width = camera.width;
			vid.height = camera.height;
			vid.visible = true;
//			vid.rotation = 90;
			addChild(vid);
//			connectRTMPServer();
		}
		private function connectRTMPServer():void {
			connection = new NetConnection();
			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			connection.client = this;
			connection.objectEncoding = ObjectEncoding.AMF0;
			connection.client = this;
			connection.proxyType = "best";
			connection.connect(serverURl);
		}
		private function netStatusHandler(event:NetStatusEvent):void {
			trace(event.info.code);
			switch (event.info.code) {
				case "NetConnection.Connect.Success":
					publishStream();
					break;
				case "NetStream.Play.StreamNotFound":
					trace("Stream not found: " + serverURl);
					break;
			}
		}
		private function publishStream():void {
			stream = new NetStream(connection);
			stream.client = this;
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.attachCamera(camera);
			stream.publish("airvstream","live");
//			addChild(video);
//			var stream:NetStream = new NetStream(connection);
//			stream.client = new CustomClient();
//			video.attachNetStream(stream);
//			stream.play(videoURL);
		}
		
		public function onBWDone():void{
			trace("onBWDone");
		}
		
		public function onBWCheck(... _args):uint{
			trace("onBWCheck");
			return (0);
		}
		
		private function handleKeyDown( e :KeyboardEvent ) :void
		{
			switch( e.keyCode )
			{
				case Keyboard.BACK:
					trace("close.....");
					NativeApplication.nativeApplication.exit();
					break;
				case Keyboard.SEARCH:
					break;
				case Keyboard.MENU:
//					e.preventDefault();
					break;
			}
		}
	}
}